package com.example.admin.mycustomview;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RecyclerView Rl_list;
    private List<String> mDatas;
    private HomeAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findById();
        inItData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    /**
     * 查找组件
     */
    private void findById() {
        Rl_list = (RecyclerView) findViewById(R.id.Rl_list);

    }

    /**
     * 初始化
     */
    private void inItData() {

        mDatas = new ArrayList<String>();
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--图片缩放");
        mDatas.add("自定义组件--绘制直尺");
        mDatas.add("自定义组件--画布缩放");
        mDatas.add("自定义组件--画布缩放2");
        mDatas.add("自定义组件--表盘绘制");
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--码表");
        mDatas.add("自定义组件--码表");


//        Rl_list.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));//瀑布流
//        Rl_list.setLayoutManager(new GridLayoutManager(this, 4));//网格
        Rl_list.setLayoutManager(new LinearLayoutManager(this));//ListView

        Rl_list.setAdapter(mAdapter = new HomeAdapter());
        Rl_list.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST));
//        Rl_list.addItemDecoration(new DividerGrideItemDecoration(this));//网格式布局设置分割线

        mAdapter.setOnItemClickLitener(new OnItemClickLitener() {

            @Override
            public void onItemClick(View view, int position) {

                switch (position) {
                    case 0:
                        startActivity(new Intent(MainActivity.this, FirstActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(MainActivity.this, TwoActivity.class));
                        break;
                    case 2:
                        startActivity(new Intent(MainActivity.this, ThreeActivity.class));
                        break;
                    case 4:
                        startActivity(new Intent(MainActivity.this, FourActivity.class));
                        break;
                    case 5:
                        startActivity(new Intent(MainActivity.this, FiveActivity.class));
                        break;
                    case 6:
                        startActivity(new Intent(MainActivity.this, SixActivity.class));
                        break;
                }

            }
        });

    }

    class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {


        private OnItemClickLitener mOnItemClickLitener;

        public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
            this.mOnItemClickLitener = mOnItemClickLitener;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_home, parent, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, int position) {
            holder.tv.setText(mDatas.get(position));

            // 如果设置了回调，则设置点击事件
            if (mOnItemClickLitener != null) {
                holder.tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(holder.itemView, pos);
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tv;

            public MyViewHolder(View view) {
                super(view);
                tv = (TextView) view.findViewById(R.id.id_num);
            }
        }

    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }
}
